<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\FriendlyLinkRepository;
use App\Http\Servers\Admin\FriendlyLinkServer;
use App\Http\Servers\Admin\ImageServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class FriendlyLinkController extends CommonController
{
    //友情链接列表
    public function friendlylink(Request $request, FriendlyLinkRepository $friendlyLinkRepository, FriendlyLinkServer $friendlyLinkServer)
    {
        if ($request->has('status')) {
            $friendlyLink = \App\FriendlyLink::findorfail($request->status);

            if ($message = $friendlyLinkServer->nopower($friendlyLink->author)) {
                return back()->with($message);
            }

            $friendlyLink['status'] = (bool)$friendlyLink['status'] ? 0 : 1;
            $result = $friendlyLink->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', '友情链接 ' . $friendlyLink->name . ' 状态修改成功!');
            } else {
                $message['message'] = message(false, '修改失败', '友情链接 ' . $friendlyLink->name . ' 状态修改失败!');
            }
            return back()->with($message);
        }

        if ($request->has('del')) {
            $friendlyLink = \App\FriendlyLink::findorfail($request->del);

            if ($message = $friendlyLinkServer->nopower($friendlyLink->author)) {
                return back()->with($message);
            }

            $result = $friendlyLink->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', '友情链接 ' . $friendlyLink->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', '友情链接 ' . $friendlyLink->name . ' 删除失败!');
            }
            return back()->with($message);
        }

        $data['friendlyLinks'] = $friendlyLinkRepository->getFriendlyLink();

        return view('admin.friendlylink.friendlylink', $data);
    }

    //友情链接添加
    public function friendlylink_add(Request $request, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required',
                'url' => 'required|url',
                'xindex' => 'required',
            ]);

            if ($request->hasFile('image')) {
                $create['image'] = $imageServer->uploadImage($request->image, 'friendly_link', 'friendly_link');
            }
            $create['name'] = $request->name;
            $create['url'] = $request->url;
            $create['xindex'] = $request->xindex;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['status'] = $request->has('status') ? 1 : 0;

            $result = \App\FriendlyLink::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', '友情链接 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '友情链接 ' . $request->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        return view('admin.friendlylink.friendlylink_add');
    }

    //友情链接编辑
    public function friendlylink_edit(Request $request, ImageServer $imageServer, FriendlyLinkServer $friendlyLinkServer)
    {
        $data['friendlyLink'] = \App\FriendlyLink::leftjoin('images', 'images.id', '=', 'friendly_links.image')
            ->select('friendly_links.*', 'images.path as image')
            ->findorfail($request->id);

        if ($request->isMethod('post')) {
            if ($message = $friendlyLinkServer->nopower($data['friendlyLink']->author)) {
                return back()->with($message);
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'url' => 'required|url',
                'xindex' => 'required',
            ]);

            if ($request->hasFile('image')) {
                $update['image'] = $imageServer->uploadImage($request->image, 'friendly_link', 'friendly_link');
            }

            $update['name'] = $request->name;
            $update['url'] = $request->url;
            $update['xindex'] = $request->xindex;
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\FriendlyLink::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', '友情链接 ' . $request->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '友情链接 ' . $request->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        return view('admin.friendlylink.friendlylink_edit', $data);
    }
}
